﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ZhaoPin.Feature
{
    public partial class FillTable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Timeout = 30;
            if (!IsPostBack)
            {
                RecruitContainer ec = new RecruitContainer();
                Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
                var it = from p in ec.People where p.Id.Equals(guid) select p;
                Person person = it.Single();
                tbRealName.Text = person.RealName;
                ddGenderCode.DataSource = ec.TDictGenders;
                ddGenderCode.DataTextField = "FName";
                ddGenderCode.DataValueField = "FGenderCode";
                ddGenderCode.SelectedValue = person.TDictGender.FGenderCode;
                ddNationCode.DataSource = ec.TDictNations;
                ddNationCode.DataTextField = "FName";
                ddNationCode.DataValueField = "FNationCode";
                ddNationCode.SelectedValue = person.TDictNation.FNationCode;
                DateTime birthday;
                if (DateTime.TryParse(person.IdentityCardNumber.Substring(6,8).Insert(4,"-").Insert(7,"-"), out birthday))
                {
                    tbBirthday.Text = birthday.ToShortDateString();
                }
                else
                {
                    tbBirthday.Text = "身份证号格式不正确！";
                }
                tbFirstJobDate.Text=person.FirstJobDate.HasValue?person.FirstJobDate.Value.ToString("yyyy-MM"):"";
                tbFirstJobCompany.Text = person.FirstJobCompany;
                tbIdentityCardNumber.Text = person.IdentityCardNumber;
                tbHeight.Text = person.Height.HasValue?person.Height.Value.ToString():"";
                tbCityNow.Text = person.CityNow;
                ddMarriageCode.DataSource = ec.TDictMarriages;
                ddMarriageCode.DataTextField = "FName";
                ddMarriageCode.DataValueField = "FMarriageCode";
                ddMarriageCode.SelectedValue = person.TDictMarriage.FMarriageCode;
                ddPartyCode.DataSource = ec.TDictParties;
                ddPartyCode.DataTextField = "FName";
                ddPartyCode.DataValueField = "FPartyCode";
                ddPartyCode.SelectedValue = person.TDictParty.FPartyCode;
                tbWorkedYears.Text = person.WorkedYears.HasValue ? person.WorkedYears.Value.ToString() : "";
                ddStudentCatalogCode.DataSource = ec.TDictStudentCatalogs;
                ddStudentCatalogCode.DataTextField = "FName";
                ddStudentCatalogCode.DataValueField = "FStudentCatalogCode";
                ddStudentCatalogCode.SelectedValue = person.TDictStudentCatalog.FStudentCatalogCode;
                tbPhoneNumber.Text = person.PhoneNumber;
                tbStudyDirection.Text = person.StudyDirection;
                tbTutor.Text = person.Tutor;
                ddPostGradeCode.DataSource = ec.TDictPostGrades;
                ddPostGradeCode.DataTextField = "FName";
                ddPostGradeCode.DataValueField = "FPostGradeCode";
                ddPostGradeCode.SelectedValue = person.PostGradeCode;
                ddPostCode.DataSource = ec.TDictPosts;
                ddPostCode.DataTextField = "FName";
                ddPostCode.DataValueField = "FPostCode";
                ddPostCode.SelectedValue = person.PostCode;
                ddProfessionalRankCode.DataSource = ec.TDictProfessionalRanks;
                ddProfessionalRankCode.DataTextField = "FName";
                ddProfessionalRankCode.DataValueField = "FProfessionalRankCode";
                ddProfessionalRankCode.SelectedValue = person.ProfessionalRankCode;
                DataBind();
                SetHint(person);
                ec.Dispose();
            }
        }
        protected void SetHint(Person person)
        {
            if (person.IsChecked)
            {
                if (person.IsCheckedOk)
                {
                    lbInfo.Text = "已经通过审核，报名表不能更改";
                    DisableInput();
                }
                else if (person.IsCanRefillTable)
                {
                    lbInfo.Text = "审核结果：不通过，需修改报名信息。原因：" + person.RefuseReason;
                }
                else
                {
                    lbInfo.Text = "审核结果：不通过，取消报名资格。原因：" + person.RefuseReason;
                    DisableInput();
                }
            }
        }
        protected void DisableInput()
        {
            btnSubmit.Enabled = false;
            ClientScript.RegisterClientScriptBlock(this.GetType(), "DisableInput", @" $(document).ready(function () {$('#fill input').attr('disabled', 'disabled');});$(document).ready(function () {$('#fill select').attr('disabled', 'disabled');});", true);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RecruitContainer ec = new RecruitContainer();
            Guid guid =(Guid)Membership.GetUser().ProviderUserKey;
            var it = from p in ec.People where p.Id.Equals(guid) select p;
            Person person = it.Single();
            person.GenderCode = ddGenderCode.SelectedValue;
            person.NationCode = ddNationCode.SelectedValue;
            person.MarriageCode = ddMarriageCode.SelectedValue;
            person.Birthday = DateTime.Parse(tbBirthday.Text);
            person.PartyCode = ddPartyCode.SelectedValue;
            person.FirstJobDate = DateTime.Parse(tbFirstJobDate.Text);
            person.FirstJobCompany = tbFirstJobCompany.Text;
            person.Height = int.Parse(tbHeight.Text);
            person.CityNow = tbCityNow.Text;
            person.WorkedYears = int.Parse(tbWorkedYears.Text);
            person.StudentCatalogCode = ddStudentCatalogCode.SelectedValue;
            person.PhoneNumber = tbPhoneNumber.Text;
            person.StudyDirection = tbStudyDirection.Text;
            person.Tutor = tbTutor.Text;
            person.PostGradeCode = ddPostGradeCode.SelectedValue;
            person.PostCode = ddPostCode.SelectedValue;
            person.ProfessionalRankCode = ddProfessionalRankCode.SelectedValue;

            person.IsFilledTable = true;
            person.IsChecked = false;
            ec.SaveChanges();
            ec.Dispose();
        }

        protected void fuPhoto_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            if (fuPhoto.HasFile)
            {
                if (fuPhoto.PostedFile.ContentType.Contains("jpeg"))
                {
                    if (fuPhoto.PostedFile.ContentLength < 81920)
                    {
                        RecruitContainer ec = new RecruitContainer();
                        Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
                        var it = from p in ec.People where p.Id.Equals(guid) select p;
                        Person person = it.Single();
                        person.Photo.Image = fuPhoto.FileBytes;
                        person.Photo.ContentType = fuPhoto.PostedFile.ContentType;
                        ec.SaveChanges();
                        ec.Dispose();
                    }
                }
            }

        }
    }
}